/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.center.web.action.report;

import java.text.NumberFormat;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;

import com.sinodata.bsm.center.cache.*;
import com.sinodata.bsm.center.service.event.EventService;
import com.sinodata.bsm.center.util.web.Struts2Utils;
import com.sinodata.bsm.center.web.action.BaseAction;
import com.sinodata.bsm.common.vo.*;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author zhangchao
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2013-00-00 *午00:00:00          zhangchao        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */

public class EventReportAction extends BaseAction {

    private static final Long APP_RESTYPE = 1105050000000L;

    @Autowired
    private ResCache resCache;
    //	@Autowired
    //	private ResGroupCache resGroupCache;
    //	@Autowired
    //	private  MotiResRelCache motiResRelCache;
    @Autowired
    private EventService eventService;

    /**
     * 应用系统健康报表
     * @return
     */
    public String eventReport() {

        List<Res> appList = resCache.getByResType(APP_RESTYPE);//系统

        JSONObject json = new JSONObject();
        JSONArray data = new JSONArray();
        JSONObject obj = new JSONObject();
        for (int i = 0; i < appList.size(); i++) {
            int eventnum = 0; //产生数
            int onum = 0; //关闭数
            List<Event> eventList = eventService.getNewEvents(Long.valueOf(appList.get(i).getId()));
            if (eventList != null) {
                eventnum = eventList.size();
                for (int k = 0; k < eventList.size(); k++) {
                    if (eventList.get(k).getStatus() == 0)
                        onum++;
                }
            }
            if (onum != 0) {
                NumberFormat nFormat = NumberFormat.getInstance();
                nFormat.setMaximumFractionDigits(0);
                String result = nFormat.format((float) eventnum / (float) onum * 100);
                obj.element("4", result + "%");
            } else {
                obj.element("4", 0 + "%");
            }
            obj.element("1", appList.get(i).getName());
            obj.element("2", eventnum);
            obj.element("3", onum);

            //	obj.element("5", " ");
            //	obj.element("6", " ");
            //	obj.element("7", " ");
            data.add(obj);
        }
        json.element("size", appList.size());
        json.element("data", data);

        Struts2Utils.renderText(json.toString());

        return null;
    }

    @Override
    public Object getModel() {
        return null;
    }

    @Override
    public String list() throws Exception {
        return null;
    }

    @Override
    public String input() throws Exception {
        return null;
    }

    @Override
    public String save() throws Exception {
        return null;
    }

    @Override
    public String delete() throws Exception {
        return null;
    }

    @Override
    protected void prepareModel() throws Exception {

    }

}